import Vue from 'vue'
import App from './App.vue'
import vueRouter from 'vue-router'
import Login from './login.vue'
import Home from './home.vue'
import User from './component/User.vue'
import UserAdd from './component/UserAdd.vue'
import Role from './component/Role.vue'
import RoleAdd from './component/RoleAdd.vue'
import axios from 'axios';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VueRouter from 'vue-router';
import store from './store'
Vue.use(ElementUI);
Vue.use(vueRouter);
axios.defaults.withCredentials = true
Vue.prototype.$ajax = axios;
const routes = [
  {
    path: '/', component: Login
  },
  {
    path: '/home', component: Home,
    children: [
      {
        path: '/', name: '系统', component: Home, children: [
          {
            path: '/user', component: User, name: ''
          },
          {
            path: '/role', component: Role, name: ''
          },
          {
            path: '/userAdd', component: UserAdd
          },
          {
            path: '/roleAdd', component: RoleAdd
          }
        ]
      }
    ]
  }
]

const router = new VueRouter({ routes })
router.beforeEach((to, from, next) => {
  if (to.path == '/') {
    next();
    return
  }
  var username = store.state.user.username;
  console.log(username);
  if (username == '尚未登录') {
    next({ path: '/' });
    return;
  }
  console.log("hhhhhhhh{}", store.state.rs);
  if (store.state.rs.length == 0) {
    console.log("000000000000000000000000000000000{}", store.state.rs);
    initMenu(router, store);
    setTimeout(() => {
      next();
    }, 1000);
    return;
  }
  next();
})

export const initMenu = (router, store) => {
  axios({
    url: "http://localhost:8081/menu/getMenuByUserID",
    method: "get"
  }).then(function (param) {
    console.log("<><><><>{}", param);

    if (param) {
      var routes = formRouters(param.data.object);
      console.log("routests为:{}", routes);
      //把vue路由的数组赋值给 vue router对象
      router.options.routes[1].children = routes;
      console.log("-----{}", router.options.routes[1].children);
      store.commit('initMenu', routes);
    }
  })
}

export const formRouters = (routers) => {

  // console.log('=============================>{}',routers);
  let array = [];
  //json 数据转成vue 识别的路由格式
  routers.forEach(element => {
    let children = [];
    if (element.menuList && element.menuList instanceof Array) {
      children = formRouters(element.menuList);
    }
    let r = {
      path: element.path,
      component(resolve) {
        if (element.component.startsWith('User')) {
          require(['./component/' + element.component + '.vue'], resolve)
        } else if (element.component.startsWith('Role')) {
          require(['./component/' + element.component + '.vue'], resolve)
        }
      },
      name: element.name,
      children: children
    }
    array.push(r);
  });
  return array;
}

export const yhlist = (name) => {
  axios({
    url: "http://localhost:8081/user/page",
    method: "get"
  }).then(function (param) {
    console.log("<><><><>{}", param);
    if (param) {
      var routes = formRouters(param.data.object);
      console.log("routests为:{}", routes);
      //把vue路由的数组赋值给 vue router对象
      router.options.routes[1].children = routes;
      console.log("-----{}", router.options.routes[1].children);
      store.commit('initMenu', routes);
    }
  })
}



new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
